package com.zc.http.processor.core.advice;

import com.zc.commons.http.entity.Response;
import com.zc.commons.http.enums.RespCode;
import com.zc.commons.lang.exception.BaseHandleException;
import com.zc.commons.lang.exception.MsgHandlerException;
import com.zc.commons.lang.exception.RemoteException;
import com.zc.commons.lang.exception.ValidationException;
import com.zc.http.processor.common.constant.MessageConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 响应异常处理
 *
 * @author zcc
 */
@Slf4j
@RestControllerAdvice
public class ResponseExceptionAdvice {

    @ExceptionHandler(value = {MsgHandlerException.class,
            BaseHandleException.class, ValidationException.class, RemoteException.class})
    public Response<String> handleMessage(RuntimeException e) {
        if (e.getCause() != null) {
            log.error(e.getMessage(), e.getCause());
        }
        return Response.error(RespCode.INTERNAL_ERROR, e.getMessage());
    }

    @ExceptionHandler(value = {Exception.class})
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public Response<String> handleException(Exception e) {
        log.error(MessageConstant.INTERNAL_ERROR, e);
        return Response.error(RespCode.INTERNAL_ERROR, e.getMessage());
    }


}
